//
//  AlarmDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/23.
//

import Foundation
import UIKit

class AlarmDetailView: UIScrollView {
    let basicV = AlarmBasicView()
    let submitBtn = UIButton.createPrimaryLarge("处置")
    
    var modal: AlarmsModal?

    override init(frame: CGRect) {
        super.init(frame: frame)
        keyboardDismissMode = .interactive
        showsVerticalScrollIndicator = false
        bounces = true
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: AlarmsModal) {
        self.modal = modal
        basicV.updateUI(withModal: modal)
        submitBtn.isHidden = modal.status == "1"
    }
 
    func setupUI() {
        // 基本信息
        addSubview(basicV)
        basicV.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        // 处置
        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(basicV.snp.bottom).offset(12)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(-15)
        }
    }
}

class AlarmBasicView: UIView {
    let alarmDateItem = AlarmDetailItem(placeholder: "报警时间:")
    let addrItem = AlarmDetailItem(placeholder: "网点地址:")
    let imgItem = AlarmImageItem(placeholder: "报警图片:")
    let videoItem = AlarmVideoItem(placeholder: "报警视频:")
    let complanyItem = AlarmDetailItem(placeholder: "保安公司:")
    let compTeleItem = AlarmDetailItem(placeholder: "保安公司电话:")
    let liableItem = AlarmDetailItem(placeholder: "场所负责人:")
    let contractorItem = AlarmDetailItem(placeholder: "安保负责人:")
    let disposalItem = AlarmDetailItem(placeholder: "处置结果:")
    let disposalDateItem = AlarmDetailItem(placeholder: "处置时间:")
    let teleItem = AlarmDetailItem(placeholder: "监控中心电话:")
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        layer.cornerRadius = 10
        layer.masksToBounds = true
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: AlarmsModal) {
        alarmDateItem.value = modal.tbAlarmTime
        addrItem.value = modal.tbWebsiteName
        disposalItem.value = modal.remark
        disposalDateItem.value = modal.updateTime
//        complanyItem.value = "XXX智能科技安保公司"
//        compTeleItem.value = "023456789"
//        liableItem.value = "张三"
//        contractorItem.value = "李四"
//        teleItem.value = "012345678"
        
        imgItem.update(withImgs: modal.tbAlarmPic ?? [])
        videoItem.update(withURLs: modal.tbAlarmVideo ?? [])
    }
    
    func setupUI() {
        addSubview(alarmDateItem)
        alarmDateItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(self.snp.top).offset(5)
        }

        addSubview(addrItem)
        addrItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(alarmDateItem.snp.bottom)
        }
        
        addSubview(imgItem)
        imgItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(addrItem.snp.bottom).offset(6)
        }
        
        addSubview(videoItem)
        videoItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(imgItem.snp.bottom).offset(6)
        }
        
        complanyItem.isHidden = true
        addSubview(complanyItem)
        complanyItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(videoItem.snp.bottom)
            make.height.equalTo(0)
        }
        
        compTeleItem.isHidden = true
        addSubview(compTeleItem)
        compTeleItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(complanyItem.snp.bottom)
            make.height.equalTo(0)
        }
        
        liableItem.isHidden = true
        addSubview(liableItem)
        liableItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(compTeleItem.snp.bottom)
            make.height.equalTo(0)
        }
        
        contractorItem.isHidden = true
        addSubview(contractorItem)
        contractorItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(liableItem.snp.bottom)
            make.height.equalTo(0)
        }
        
        addSubview(disposalItem)
        disposalItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(contractorItem.snp.bottom)
        }
        
        addSubview(disposalDateItem)
        disposalDateItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(disposalItem.snp.bottom)
        }
        
        teleItem.isHidden = true
        addSubview(teleItem)
        teleItem.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(disposalDateItem.snp.bottom)
            make.bottom.equalToSuperview().offset(-5)
            make.height.equalTo(0)
        }
    }
}

class AlarmDetailItem: UIView {
    let keyL = UILabel()
    let valueL = UILabel()
    
    let subValueBtn = UIButton(type: .custom)

    var keyWidth = 96.0
    var placeholder: String = "" {
        didSet {
            keyL.text = placeholder
        }
    }
    
    var value: String? = "--" {
        didSet {
            valueL.text = value ?? "--"
        }
    }

    var textAlignment: NSTextAlignment = NSTextAlignment.right {
        didSet {
            keyL.textAlignment = textAlignment
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    init(withKeyWidth width: CGFloat? = nil, placeholder: String? = nil) {
        super.init(frame: .zero)
        if width != nil { self.keyWidth = width! }
        setupUI()
        if placeholder != nil {
            self.placeholder = placeholder!
            keyL.text = placeholder!
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(withModal modal: RepairDetailModal) {
        keyL.text = modal.key
        valueL.text = modal.val
        subValueBtn.isHidden = modal.subVal == nil
        if modal.subVal != nil {
            subValueBtn.setTitle("\(modal.subVal!)", for: .normal)
        }
    }
    
    // MARK: - Setup
    func setupUI() {
        keyL.textColor = .hex("#666666")
        keyL.font = .systemFont(ofSize: 14)
        keyL.textAlignment = .right
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left).offset(20)
            make.top.equalToSuperview().offset(16)
            make.width.equalTo(keyWidth)
        }
        
        valueL.text = placeholder
        valueL.font = .systemFont(ofSize: 15)
        valueL.numberOfLines = 0
        addSubview(valueL)
        valueL.snp.makeConstraints { make in
            make.top.equalTo(keyL.snp.top)
            make.left.equalTo(keyL.snp.right).offset(14)
            make.right.equalToSuperview().offset(-14)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        subValueBtn.titleLabel?.font = .systemFont(ofSize: 15)
        subValueBtn.setTitleColor(.primary, for: .normal)
        addSubview(subValueBtn)
        subValueBtn.snp.makeConstraints { make in
            make.centerY.equalTo(keyL.snp.centerY)
            make.left.equalTo(keyL.snp.right).offset(14)
            make.height.equalTo(40)
        }
        
        let line = UIView.createLine()
        line.backgroundColor = .hex("#F1F1F1")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}

class AlarmImageItem: UIView {
    let keyL = UILabel()
    let collectionV = SEImageCollectionView()

    var keyWidth = 96.0
    var placeholder: String = "附件" {
        didSet {
            keyL.text = placeholder
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    init(withKeyWidth width: CGFloat? = nil, placeholder: String? = nil) {
        super.init(frame: .zero)
        if width != nil { self.keyWidth = width! }
        setupUI()
        if placeholder != nil {
            self.placeholder = placeholder!
            keyL.text = placeholder!
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(withImgs imgs: [String]) {
        collectionV.imgs = imgs
        let count = CGFloat(imgs.count / 5 + 1)
        collectionV.snp.updateConstraints { make in
            make.height.equalTo(65 * count)
        }
    }
    
    // MARK: - Setup
    func setupUI() {
        keyL.textColor = .hex("#666666")
        keyL.font = .systemFont(ofSize: 14)
        keyL.textAlignment = .right
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(16)
            make.left.equalTo(self.snp.left).offset(20)
            make.width.equalTo(keyWidth)
//            make.height.equalTo(20)
        }
        
        collectionV.itemHeight = 55
        addSubview(collectionV)
        collectionV.snp.makeConstraints { (make) in
            make.top.equalTo(keyL.snp.top)
            make.left.equalTo(keyL.snp.right).offset(14)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(55)
        }
        
        let line = UIView.createLine()
        line.backgroundColor = .hex("#F1F1F1")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}

class AlarmVideoItem: UIView {
    let keyL = UILabel()
    let collectionV = SEVideoCollectionView()

    var keyWidth = 96.0
    var placeholder: String = "视频" {
        didSet {
            keyL.text = placeholder
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    init(withKeyWidth width: CGFloat? = nil, placeholder: String? = nil) {
        super.init(frame: .zero)
        if width != nil { self.keyWidth = width! }
        setupUI()
        if placeholder != nil {
            self.placeholder = placeholder!
            keyL.text = placeholder!
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(withURLs urls: [String]) {
        collectionV.urls = urls
        let count = CGFloat(urls.count / 5 + 1)
        collectionV.snp.updateConstraints { make in
            make.height.equalTo(65 * count)
        }
    }
    
    // MARK: - Setup
    func setupUI() {
        keyL.textColor = .hex("#666666")
        keyL.font = .systemFont(ofSize: 14)
        keyL.textAlignment = .right
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(16)
            make.left.equalTo(self.snp.left).offset(20)
            make.width.equalTo(keyWidth)
//            make.height.equalTo(20)
        }
        
        collectionV.itemHeight = 55
        addSubview(collectionV)
        collectionV.snp.makeConstraints { (make) in
            make.top.equalTo(keyL.snp.top)
            make.left.equalTo(keyL.snp.right).offset(14)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(55)
        }
        
        let line = UIView.createLine()
        line.backgroundColor = .hex("#F1F1F1")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
}
